package io.github.idol.roomlist.room

import android.databinding.DataBindingUtil
import android.databinding.ViewDataBinding
import android.support.v7.recyclerview.extensions.ListAdapter
import android.view.LayoutInflater
import android.view.ViewGroup
import io.github.idol.Constants
import io.github.idol.DataBindingViewHolder
import io.github.idol.R
import io.github.idol.data.ExtInfo
import io.github.idol.data.RoomDetailInfo
import io.github.idol.databinding.ListItemRoomTextBinding
import io.github.idol.roomlist.room.viewmodel.ItemFanpaiViewModel
import io.github.idol.roomlist.room.viewmodel.ItemTextViewModel

/**
 * Created by codeczx on 2018/8/8 下午 09:10.
 * Class description:
 */

class RoomDetailAdapter(roomDetailDiffCallback: RoomDetailDiffCallback): ListAdapter<ExtInfo, DataBindingViewHolder>(roomDetailDiffCallback) {

    override fun getItemViewType(position: Int): Int {
        val extInfo = getItem(position)
        when(extInfo.messageObject){
            Constants.MESSAGE_TYPE_TEXT->return R.layout.list_item_room_text
            Constants.MESSAGE_TYPE_FANPAI_TEXT->return R.layout.list_item_room_fanpai
        }
        return R.layout.list_item_room_text
    }

    override fun onBindViewHolder(holder: DataBindingViewHolder, position: Int) {
        when(getItemViewType(position)){
            R.layout.list_item_room_text->holder.bind(ItemTextViewModel(getItem(position)))
            R.layout.list_item_room_fanpai->holder.bind(ItemFanpaiViewModel(getItem(position)))
        }
//        holder.bind(ItemTextViewModel(getItem(position)))
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int):DataBindingViewHolder {
        val databinding :ViewDataBinding = DataBindingUtil.inflate(LayoutInflater.from(parent.context),viewType,parent,false)
        return DataBindingViewHolder(databinding)
    }

}